home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 726-750 / 741 / rkrm_lib1 / rkrm_lib1.lha / Intuition / Gadgets / updatestrgad.c < prev   
C/C++ Source or Header  |  1992-09-03  |  7KB  |  218 lines

  1. ;/* updatestrgad.c - Execute me to compile me with SAS C 5.10
  2. LC -b1 -cfistq -v -y -j73 updatestrgad.c
  3. Blink FROM LIB:c.o,updatestrgad.o TO updatestrgad LIBRARY LIB:LC.lib,LIB:Amiga.lib
  4. quit
  5. */
  6.  
  7. /*
  8. Copyright (c) 1992 Commodore-Amiga, Inc.
  9.  
  10. This example is provided in electronic form by Commodore-Amiga, Inc. for
  11. use with the "Amiga ROM Kernel Reference Manual: Libraries", 3rd Edition,
  12. published by Addison-Wesley (ISBN 0-201-56774-1).
  13.  
  14. The "Amiga ROM Kernel Reference Manual: Libraries" contains additional
  15. information on the correct usage of the techniques and operating system
  16. functions presented in these examples.  The source and executable code
  17. of these examples may only be distributed in free electronic form, via
  18. bulletin board or as part of a fully non-commercial and freely
  19. redistributable diskette.  Both the source and executable code (including
  20. comments) must be included, without modification, in any copy.  This
  21. example may not be published in printed form or distributed with any
  22. commercial product.  However, the programming techniques and support
  23. routines set forth in these examples may be used in the development
  24. of original executable software products for Commodore Amiga computers.
  25.  
  26. All other rights reserved.
  27.  
  28. This example is provided "as-is" and is subject to change; no
  29. warranties are made.  All use is at your own risk. No liability or
  30. responsibility is assumed.
  31. */
  32.  
  33. /*
  34. ** updatestrgad.c - Show the use of a string gadget.  Shows both the use of
  35. ** ActivateGadget() and how to properly modify the contents of a string gadget.
  36. */
  37. #define INTUI_V36_NAMES_ONLY
  38.  
  39. #include <exec/types.h>
  40. #include <intuition/intuition.h>
  41. #include <intuition/intuitionbase.h>
  42.  
  43. #include <clib/exec_protos.h>
  44. #include <clib/dos_protos.h>
  45. #include <clib/intuition_protos.h>
  46.  
  47. #include <string.h>
  48. #include <stdio.h>
  49.  
  50. #ifdef LATTICE
  51. int CXBRK(void)    { return(0); }  /* Disable Lattice CTRL/C handling */
  52. int chkabort(void) { return(0); }  /* really */
  53. #endif
  54.  
  55. /* our function prototypes */
  56. VOID updateStrGad(struct Window *win, struct Gadget *gad, UBYTE *newstr);
  57. VOID handleWindow(struct Window *win, struct Gadget *gad);
  58.  
  59. struct Library *IntuitionBase;
  60.  
  61. /* NOTE that the use of constant size and positioning values are
  62. ** not recommended; it just makes it easy to show what is going on.
  63. ** The position of the gadget should be dynamically adjusted depending
  64. ** on the height of the font in the title bar of the window.  This
  65. ** example adapts the gadget height to the screen font. Alternately,
  66. ** you could specify your font under V37 with the StringExtend structure.
  67. */
  68. #define BUFSIZE (100)
  69. #define MYSTRGADWIDTH (200)
  70. #define MYSTRGADHEIGHT (8)
  71.  
  72. UWORD strBorderData[] =
  73.     {
  74.     0,0, MYSTRGADWIDTH + 3,0, MYSTRGADWIDTH + 3,MYSTRGADHEIGHT + 3,
  75.     0,MYSTRGADHEIGHT + 3, 0,0,
  76.     };
  77. struct Border strBorder =
  78.     {
  79.     -2,-2,1,0,JAM1,5,strBorderData,NULL,
  80.     };
  81. UBYTE strBuffer[BUFSIZE];
  82. UBYTE strUndoBuffer[BUFSIZE];
  83. struct StringInfo strInfo =
  84.     {
  85.     strBuffer,strUndoBuffer,0,BUFSIZE, /* compiler sets remaining fields to zero */
  86.     };
  87. struct Gadget strGad =
  88.     {
  89.     NULL, 20,20,MYSTRGADWIDTH,MYSTRGADHEIGHT,
  90.     GFLG_GADGHCOMP, GACT_RELVERIFY | GACT_STRINGCENTER,
  91.     GTYP_STRGADGET, &strBorder, NULL, NULL,0,&strInfo,0,NULL,
  92.     };
  93.  
  94. #define ANSCNT 4
  95. UBYTE *answers[ANSCNT] = {"Try again","Sorry","Perhaps","A Winner"};
  96. int ansnum = 0;
  97. UBYTE *activated_txt = "Activated";
  98.  
  99. /*   main - show the use of a string gadget.
  100. */
  101. VOID main(int argc, char **argv)
  102. {
  103. struct Window *win;
  104.  
  105. /* make sure to get intuition version 37, for OpenWindowTags() */
  106. IntuitionBase = OpenLibrary("intuition.library", 37);
  107. if (IntuitionBase)
  108.     {
  109.     /* Load a value into the string gadget buffer.
  110.     ** This will be displayed when the gadget is first created.
  111.     */
  112.     strcpy(strBuffer, "START");
  113.  
  114.     if (win = OpenWindowTags(NULL,
  115.                             WA_Width, 400,
  116.                             WA_Height, 100,
  117.                             WA_Title,"Activate Window, Enter Text",
  118.                             WA_Gadgets, &strGad,
  119.                             WA_CloseGadget, TRUE,
  120.                             WA_IDCMP, IDCMP_ACTIVEWINDOW |
  121.                                 IDCMP_CLOSEWINDOW | IDCMP_GADGETUP,
  122.                             TAG_END))
  123.         {
  124.         handleWindow(win,&strGad);
  125.  
  126.         CloseWindow(win);
  127.         }
  128.     CloseLibrary(IntuitionBase);
  129.     }
  130. }
  131.  
  132.  
  133.  
  134. /*
  135. ** Process messages received by the window.  Quit when the close gadget
  136. ** is selected, activate the gadget when the window becomes active.
  137. */
  138. VOID handleWindow(struct Window *win, struct Gadget *gad)
  139. {
  140. struct IntuiMessage *msg;
  141. struct Gadget *gadget;
  142. ULONG  class;
  143.  
  144. for (;;)
  145.     {
  146.     Wait(1L << win->UserPort->mp_SigBit);
  147.     while (msg = (struct IntuiMessage *)GetMsg(win->UserPort))
  148.         {
  149.         /* Stash message contents and reply, important when message
  150.         ** triggers some lengthy processing
  151.         */
  152.         class = msg->Class;
  153.         /* If it's a gadget message, IAddress points to Gadget */
  154.         if((class == IDCMP_GADGETUP)||(class == IDCMP_GADGETDOWN))
  155.                 gadget = (struct Gadget *)msg->IAddress;
  156.         ReplyMsg((struct Message *)msg);
  157.  
  158.         switch (class)
  159.             {
  160.             case IDCMP_ACTIVEWINDOW:
  161.                 /* activate the string gadget.  This is how to activate a
  162.                 ** string gadget in a new window--wait for the window to
  163.                 ** become active by waiting for the IDCMP_ACTIVEWINDOW
  164.                 ** event, then activate the gadget.  Here we report on
  165.                 ** the success or failure.
  166.                 */
  167.                 if(ActivateGadget(gad,win,NULL))
  168.                     updateStrGad(win,gad,activated_txt);
  169.                 break;
  170.             case IDCMP_CLOSEWINDOW:
  171.                 /* here is the way out of the loop and the routine.
  172.                 ** be sure that the message was replied...
  173.                 */
  174.                 return;
  175.                 break;
  176.             case IDCMP_GADGETUP:
  177.                 /* If user hit RETURN in our string gadget for demonstration,
  178.                 ** we will change what he entered.  We only have 1 gadget,
  179.                 ** so we don't have to check which gadget.
  180.                 */
  181.                 updateStrGad(win, &strGad, answers[ansnum]);
  182.                 if(++ansnum >= ANSCNT) ansnum = 0;  /* point to next answer */
  183.                 break;
  184.             }
  185.         }
  186.     }
  187. }
  188.  
  189.  
  190.  
  191. /*
  192. ** Routine to update the value in the string gadget's buffer, then
  193. ** activate the gadget.
  194. */
  195. VOID updateStrGad(struct Window *win, struct Gadget *gad, UBYTE *newstr)
  196. {
  197. /* first, remove the gadget from the window.  this must be done before
  198. ** modifying any part of the gadget!!!
  199. */
  200. RemoveGList(win,gad,1);
  201.  
  202. /* For fun, change the value in the buffer, as well as the cursor and
  203. ** initial display position.
  204. */
  205. strcpy(((struct StringInfo *)(gad->SpecialInfo))->Buffer, newstr);
  206. ((struct StringInfo *)(gad->SpecialInfo))->BufferPos = 0;
  207. ((struct StringInfo *)(gad->SpecialInfo))->DispPos   = 0;
  208.  
  209. /* Add the gadget back, placing it at the end of the list (~0)
  210. ** and refresh its imagery.
  211. */
  212. AddGList(win,gad,~0,1,NULL);
  213. RefreshGList(gad,win,NULL,1);
  214.  
  215. /* Activate the string gadget */
  216. ActivateGadget(gad,win,NULL);
  217. }
  218.